package com.fjwt.gz.mch.ctrl.audit;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.coupon.SelfOperatedCouponsImpl;
import com.fjwt.gz.db.dto.AuAuditRecordsDTO;
import com.fjwt.gz.db.entity.AuAuditRecordsEntity;
import com.fjwt.gz.db.vo.AuAuditRecordsVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.ActivityService;
import com.fjwt.gz.service.impl.AuAuditRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 活动审批记录Controller
 *
 * @date 2025-01-07
 */
@Slf4j
@RestController
@RequestMapping("/api/auditRecords")
public class AuAuditRecordsController extends CommonCtrl {
    @Autowired
    private AuAuditRecordsService auAuditRecordsService;
    @Autowired
    protected SelfOperatedCouponsImpl selfOperatedCoupons;
    @Autowired
    private ActivityService activityService;


    /**
     * 获取审批记录列表
     *
     * @return
     */
    @PostMapping("/getList")
    public ApiRes getList() {
        AuAuditRecordsEntity entity = getObject(AuAuditRecordsEntity.class);
        AuAuditRecordsDTO auditRecordsDTO = new AuAuditRecordsDTO();
        auditRecordsDTO.setAuditSerialNo(auditRecordsDTO.getAuditSerialNo());
        auditRecordsDTO.setActivityName(entity.getActivityName());
        auditRecordsDTO.setState(entity.getState());
        IPage<AuAuditRecordsVO> page = auAuditRecordsService.getBaseMapper().selectAuditRecordsEntityPage(getIPage(), auditRecordsDTO);
        return ApiRes.page(page);
    }


    /**
     * 进行审批
     *
     * @return
     */
    @PostMapping("/audit")
    @Transactional
    public ApiRes audit() throws Exception {
        AuAuditRecordsEntity entity = getObject(AuAuditRecordsEntity.class);
        Long fansId = getCurrentUser().getFansId();
        Long userId = getCurrentUser().getUserId();
        Long awardId = getValLong("awardId");
        String appId = request.getHeader("appid");
        if (null == entity.getAuditRecordsId() || null == entity.getActivityId() || null == entity.getState() || null == entity.getRemark()) {
            return ApiRes.customFail("请求参数缺失");
        }
        return auAuditRecordsService.audits(entity, awardId, fansId, userId, appId) > 0 ? ApiRes.ok() : ApiRes.customFail("操作失败");
    }

    /**
     * 超时未审核
     */
    @PostMapping("/timeout")
    public ApiRes timeoutAuidt() {
        activityService.timeout();
        return ApiRes.ok();
    }

}
